<?php
/**
 * CollectorController is an instance of Controller (application.components).
 *
 * Controller is an instance of CController (system.web).
 * CController manages a set of actions which deal with the corresponding user requests.
 * Through the actions, CController coordinates the data flow between models and views.
 *
 * Eduow :  International system for education (http://eduow.com)
 * Copyright 2010, Nikolay Stoitsev
 *
 * Licensed under GNU General Public License version 2
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2010
 * @link          http://eduow.com Eduow Project
 * @package       eduow
 * @subpackage    application.controllers
 * @inheritance   class CollectorController » Controller » CController » CBaseController » CComponent
 * @implements
 * @since         Eduow v 0.0.3
 * @license       http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License version 2
 */

class CollectorController extends Controller{

    public $defaultAction='index';

    /**
     * Display colector's form
     */
    public function actionIndex() {
        $this->render('index');
    }

    /**
     * Process the data passed by AJAX request, prepare YQL query and format the result
     */
    public function actionProcess(){

        if(isset($_GET['search'])){
            $query = $_GET['search'];

            $queries[] = 'select Title,Description,Url,DisplayUrl '.
             'from microsoft.bing.web(20) where query="'.$query.'"';
            $queries[] = 'select title,clickurl,abstract,dispurl '.
             'from search.web(20) where query = "'.$query.'"';
            $queries[] = 'select titleNoFormatting,url,content,visibleUrl '.
             'from google.search(20) where q="'.$query.'"';
            $url = "select * from query.multi where queries='".implode($queries,';')."'";
            $api = 'http://query.yahooapis.com/v1/public/yql?q='.
            urlencode($url).'&format=json&env=store'.
       '%3A%2F%2Fdatatables.org%2Falltableswithkeys&diagnostics=false';

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $api);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);
            curl_close($ch);
        }

        $this->renderPartial('result',array('output'=>$output));

    }

}

?>
